package com.qfmy.web.app.service.impl.listenBook;

import com.qfmy.common.exception.BaseException;
import com.qfmy.common.login.LoginUser;
import com.qfmy.common.login.LoginUserHolder;
import com.qfmy.common.result.ResultCodeEnum;
import com.qfmy.model.entity.listenBook.AudioComment;
import com.qfmy.model.vo.listenBook.AudioCommentVo;
import com.qfmy.web.app.mapper.listenBook.AudioCommentMapper;
import com.qfmy.web.app.service.agent.ChatService;
import com.qfmy.web.app.service.listenBook.AudioCommentService;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 清风明月
 * @Package com.qfmy.web.app.service.impl.listenBook
 * @date 2025/11/01
 * @description 评论服务
 */
@Service
public class AudioCommentServiceImpl implements AudioCommentService {

    /**
     * 注入mapper
     */
    @Autowired
    private AudioCommentMapper audioCommentMapper;

    /**
     * 注入智能校验服务
     */
    @Autowired
    private ChatService chatService;

    /**
     * 添加评论
     * @param audioComment
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addComment(@NotNull AudioComment audioComment) {
        //获取评论的内容
        String content = audioComment.getContent();
        //调用智能校验服务
        chatService.checkByAi(content);
        //添加评论
        audioCommentMapper.addComment(audioComment);
    }

    /**
     * 判断用户是否已评论
     * @param audioId
     * @return
     */
    @Override
    public boolean isUserComment(Integer audioId) {
        //获取用户id
        LoginUser loginUser = LoginUserHolder.getLoginUser();
        if(loginUser == null)
        {
            throw new BaseException(ResultCodeEnum.TOKEN_INVALID);
        }
        //获取用户id
        Long userId = loginUser.getUserId();

        //判断用户id是否大于0
        if(userId == null || userId < 0)
        {
           return false;
        }

        //根据用户id和音频id查询评论是否存在
        return audioCommentMapper.selectUserComment(userId,audioId);
    }

    /**
     * 获取所有音频评论
     * @param audioId
     * @return
     */
    @Override
    public List<AudioCommentVo> getAllAudioComment(Integer audioId) {
        //获取所有音频评论
        List<AudioComment> list = audioCommentMapper.getAllAudioComment(audioId);
        //进行对象转换
        return convertToVo(list);
    }

    /**
     * 对象转换
     * @param list
     * @return
     */
    @Contract(pure = true)
    private List<AudioCommentVo> convertToVo(List<AudioComment> list) {
        //判断集合是否为空
        if (list != null && !list.isEmpty()) {
            return list.stream().map(audioComment -> {
                AudioCommentVo audioCommentVo = new AudioCommentVo();
                BeanUtils.copyProperties(audioComment, audioCommentVo);
                return audioCommentVo;
            }).collect(Collectors.toList());
        }
        return Collections.emptyList();
    }


}
